package chapter2;

/**
 * @author Hang
 * @date 2022/8/7
 * @project AutumnOffer
 * @description 面试题4
 **/
public class findNumberIn2DArray {
    public static void main(String[] args) {
        int[][] nums = new int[][]{{1,   4,  7, 11, 15},{2,   5,  8, 12, 19},{3,   6,  9, 16, 22},{10, 13, 14, 17, 24},{18, 21, 23, 26, 30}};
        System.out.println(findNumberIn2DArray(nums,20));
    }
    public static boolean findNumberIn2DArray(int[][] matrix, int target){
        int i=0,j = matrix[0].length-1; // 从右上角往下查找
        while(i<matrix.length&&j>=0){// 边界条件
            if(target == matrix[i][j]){ // 相等则返回
                return true;
            }else if(target > matrix[i][j]){//大于，则往下寻找
                i++;
            }else { // 小于则往左寻找
                j--;
            }
        }
        return false; // 没找到
    }
}
